package com.smartum.mpr.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotCorrectPermission;
import com.smartum.mpr.exception.NotExistsHoaxException;
import com.smartum.mpr.model.Hoax;
import com.smartum.mpr.model.Odd;
import com.smartum.mpr.model.UploadedImageHoax;

@Repository
public class UploadedImageHoaxDao extends GenericDao<Long, UploadedImageHoax> {

	public Hoax getHoax(Long userId, Long imageId) throws NotExistsHoaxException, DatabaseIntegrityException{
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImageHoax.class);
		criteria.createCriteria("user").add(Restrictions.eq("id", userId));
		criteria.createCriteria("image").add(Restrictions.eq("id", imageId));

		List hoaxs = hibernateTemplate.findByCriteria(criteria);
		if (hoaxs.size() == 0) {
			throw new NotExistsHoaxException();
		} else if (hoaxs.size() > 1) {
			throw new DatabaseIntegrityException("database has few equals hoaxs");
		} else if (hoaxs.size() == 1) {
			return (Hoax) hoaxs.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	@Override
	protected Class<UploadedImageHoax> getEntityClass() {
		return UploadedImageHoax.class;
	}
}
